package cn.hkfdt.mybatis.comment.generator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

/**
 * Created by hongshu on 2016/11/25.
 */
public class HkfdtCommentGenerator extends DefaultCommentGenerator {
    public HkfdtCommentGenerator() {
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().equals("")) {
            // 添加字段注释
            StringBuffer sb = new StringBuffer();
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * <pre>");
            if (introspectedColumn.getRemarks() != null)
                field.addJavaDocLine(" * 表字段注释 : " + introspectedColumn.getRemarks());
            sb.append(" * 表字段 : ");
            sb.append(introspectedTable.getFullyQualifiedTable());
            sb.append('.');
            sb.append(introspectedColumn.getActualColumnName());
            field.addJavaDocLine(sb.toString());
            field.addJavaDocLine(" * </pre>");
            field.addJavaDocLine(" */");
            StringBuffer annotationConfig = new StringBuffer();
            annotationConfig.append("@ApiModelProperty(value =\"");
            if (introspectedColumn.getRemarks() != null)
                annotationConfig.append(introspectedColumn.getRemarks());
            annotationConfig.append("\")");
            field.addAnnotation(annotationConfig.toString());


        }
    }


    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
//        topLevelClass.addImportedType("io.swagger.annotations.ApiModel");
        StringBuilder sb = new StringBuilder();
        topLevelClass.addJavaDocLine("/**");
        String remarks = introspectedTable.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            topLevelClass.addJavaDocLine(" * Database Table Remarks:");
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            String[] var6 = remarkLines;
            int var7 = remarkLines.length;
            for (int var8 = 0; var8 < var7; ++var8) {
                String remarkLine = var6[var8];
                topLevelClass.addJavaDocLine(" *   " + remarkLine);
            }
        }

        topLevelClass.addJavaDocLine(" *");
        sb.append(" * the database table ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(sb.toString());
        topLevelClass.addJavaDocLine(" * create by Hong Shu");
        topLevelClass.addJavaDocLine(" */");

        /*StringBuffer annotationConfig = new StringBuffer();
        annotationConfig.append("@ApiModel(value =\"");
        if (introspectedTable.getRemarks() != null)
            annotationConfig.append(introspectedTable.getRemarks());
        annotationConfig.append("\")");
        topLevelClass.addAnnotation(annotationConfig.toString());*/
    }
}
